﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;
using System.Drawing.Printing;
using System.ComponentModel;
using DevExpress.XtraEditors.Registrator;

namespace ProgNetDxComponents.Controls.Editors
{
    public class PrinterComboBoxEdit : ComboBoxEdit
    {
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        Category("Properties")]
        public new RepositoryItemPrinterComboBox Properties
        {
            get { return fProperties as RepositoryItemPrinterComboBox; }
        }
         
        protected override void OnPopupShown()
        {
            //Properties.GetPrinters();
            base.OnPopupShown();
        }
        protected override void CreateRepositoryItem()
        {
            fProperties = new RepositoryItemPrinterComboBox(this);
        }
    }

    [UserRepositoryItem("Register")]
    public class RepositoryItemPrinterComboBox : RepositoryItemComboBox
    {
        static RepositoryItemPrinterComboBox()
        {
            Register();
        }
        public static void Register()
        {
            EditorRegistrationInfo.Default.Editors.Add(
                new EditorClassInfo("PrinterComboBoxEdit", typeof(PrinterComboBoxEdit),
                    typeof(RepositoryItemPrinterComboBox),
                    typeof(DevExpress.XtraEditors.ViewInfo.LookUpEditViewInfo),
                    new DevExpress.XtraEditors.Drawing.ButtonEditPainter(),
                    true,
                    null));
        }

        public RepositoryItemPrinterComboBox()
        {
            this.QueryPopUp += new CancelEventHandler(RepositoryItemPrinterComboBox_QueryPopUp);
            GetPrinters();
        }

        void RepositoryItemPrinterComboBox_QueryPopUp(object sender, CancelEventArgs e)
        {
            GetPrinters();
        }
        public RepositoryItemPrinterComboBox(BaseEdit edit)
        {
            this.QueryPopUp += new CancelEventHandler(RepositoryItemPrinterComboBox_QueryPopUp);
            GetPrinters();
            SetOwnerEdit(edit);
        }
        public override TextEditStyles TextEditStyle
        {
            get
            {
                return TextEditStyles.DisableTextEditor;
            }
            set
            {
                //base.TextEditStyle = value;
            }
        }

        internal void GetPrinters()
        {
            if (this.IsAppRunning())
            {
                List<string> printers2 = new List<string>();
                List<string> printers = new List<string>();
                foreach (object i in Items)
                {
                    printers.Add(i.AsString());
                }
                foreach (string s in PrinterSettings.InstalledPrinters)
                    printers2.Add(s);
                if (printers.Count != printers2.Count ||
                    printers.AsSingleString(",") != printers2.AsSingleString(","))
                {
                    Items.Clear();
                    Items.AddRange(printers2.ToArray());
                }
            }
        }
    }
}
